import { handleTime } from "../utils/commonFunc";
import Toast from '../../miniprogram_npm/vant-weapp/toast/toast';

const db = wx.cloud.database();
const App = getApp();
const navLists_col = db.collection('navLists');
const goods_col = db.collection('goods');
const hotDatas_col = db.collection('hotDatas');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 1,     //导航栏默认选中的tab
    navDatas: [],  //导航栏信息
    navScrollLeft: 0,  //设置横向滚动条位置
    imgSrc: "../../images/down.png",
    sureNum: 1,   //下拉栏标识
    isShow: true,  //下拉框的显示与隐藏
    hotLists: [],   //热榜数据
    newDatas: [],  //头条新闻数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadNavList();
    this.loadNewList();
    // 获取系统信息
    wx.getSystemInfo({
      success: (res) => {
          this.setData({
              windowWidth: res.windowWidth
          })
      },
    });
    // 状态栏样式
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#ffffff',
    });
    // 导航栏数据
    this.setData({
      navHeight: App.globalData.navHeight,
      navTop: App.globalData.navTop,
      ztlHeight: App.globalData.statusBarHeight,
    })       
    },

  // 拿到导航栏数据
  async loadNavList() {
    let res = await navLists_col.get();
    this.setData({
      navDatas: res.data
    })
  },
    
// 用户切换选项卡时触发
  handler(event){
    var index = event.currentTarget.dataset.index;
    this.setData({
        active: index,
        isShow: true
    })
  },

// 点击导航栏下拉框
  changesImg() {
    if(this.data.sureNum === 1){
      this.setData({
        imgSrc: "../../images/up.png",
        sureNum: 2,
        isShow: false
      })
    } else {
      this.setData({
        imgSrc: "../../images/down.png",
        sureNum: 1,
        isShow: true
      })
    }
  },

// 点击模态框关闭
  handleView() {
    this.setData({
      imgSrc: "../../images/down.png",
      isShow: true,
      sureNum: 1,
    })
  },

// 拿到新闻数据
  async loadNewList() {
    let resOne = await goods_col.get();
    let resTwo = await hotDatas_col.get();

    resOne.data.forEach(item => {
      let a = new Date(item.showTime).getTime();
      item.showTime = handleTime(a);
    });
    resTwo.data.forEach(item => {
      let b = new Date(item.showTime).getTime();
      item.showTime = handleTime(b);
    });
    this.setData({
      newDatas: resOne.data,
      hotLists: resTwo.data
    })
  },

// 新闻信息滑块切换
  swiperChange: function (e) {
    var cur = e.detail.current;
    var singleNavWidth = this.data.windowWidth / 6;
    this.setData({
      active: e.detail.current,
      navScrollLeft: (cur - 3) * singleNavWidth
    })
  },

// 点击新闻跳转到详情
  selectOne: function (event) {
    Toast.loading({
      duration: 1100,
      message: '加载中...',
      forbidClick: true,
      onClose: () => {
        // 点击新闻列表项，携带id跳转新闻详情页面
        var id = event.currentTarget.dataset.detailid;
        var newSign = event.currentTarget.dataset.sign;

        wx.navigateTo({
          url: '/pages/details/details?id='+id + '&sign=' +newSign,
        })
      },
    });
  },





})